<?php

/**
 * Created by PhpStorm.
 * User: GadflyBSD
 * Date: 16/3/12
 * Time: 11:36
 */
class BusinessAction extends CommonAction{
	private $pnr;

	public function _initialize(){
		parent::_initialize();
		Vendor('ChinaPnr');
		$this->pnr = new ChinaPnr('finance');
	}

	public function index(){
		header("Location: ".$this->weixin->get_auth2_url(APP_URL.U('Business/run', array('an' => I('get.an')))));
	}

	public function onePage(){
		if($this->checkWeixin()){
			switch(I('get.type')){
				case 'confirm':
					$order = M('WangkaOrder')->where('uuid="'.I('get.id').'"')->find();
					$order['status'] = (string)$order['status_flag'];
					if($order['status'] == '0'){
						$this->assign(M('WangkaBindView')->where('uid='.$order['uid'])->field('nickname, title')->find());
						$order['id'] = substr_replace($order['uuid'], '****', 8, 18);
						$this->assign($order);
						$this->display('confirm');
					}else{
						$this->message('error', '该订单已经被操作过, 请不要对相同订单进行重复操作!');
					}
					break;
				case 'success':
					$this->message('success', '操作已经成功, 点击确定关闭该页面!');
					break;
			}
		}else{
			header("Location: ".$this->weixin->get_auth2_url(APP_URL.__SELF__, 'snsapi_userinfo'));
		}
	}

	public function shopList(){
		$this->assign(array(
			'category'  => D('Business')->getCategory(I('get.cid')),
			'all_url'   => U('Business/shopList', array('cid' => I('get.cid'))),
			'list'      => D('Business')->getShopList(I('get.cid')),
		));
		$this->display();
	}

	public function shopInfo(){
		$this->assign(array(
			'info'  => D('Business')->getShopInfo(I('get.sid')),
			'back'  => U('Business/shopList', array('cid' => I('get.cid')))
		));
		$this->display();
	}

	/**
	 * 订阅行为响应和自动回复消息
	 */
	public function replay(){
		Vendor('Weixin');
		$weixin = new Weixin('WKSHOP');
		//$weixin->valid();
		if($weixin->checkSignature()){
			$data = $weixin->request();
			if($data && is_array($data)){
				switch($data['MsgType']){
					case 'text':
						$resuil = D('Public')->attention($data['FromUserName'], 'subscribe');
						$weixin->response($this->sendNews(), 'news');
						break;
					case 'image':
						$resuil = D('Public')->attention($data['FromUserName'], 'subscribe');
						$weixin->response($this->sendNews(), 'news');
						break;
					case 'voice':
						$resuil = D('Public')->attention($data['FromUserName'], 'subscribe');
						$weixin->response($this->sendNews(), 'news');
						break;
					case 'video':
						$resuil = D('Public')->attention($data['FromUserName'], 'subscribe');
						$weixin->response($this->sendNews(), 'news');
						break;
					case 'event':
						switch($data['Event']){
							case 'subscribe':
								$resuil = D('Public')->attention($data['FromUserName'], 'subscribe');
								$weixin->response($this->sendNews(), 'news');
								break;
							case 'unsubscribe':
								$resuil = D('Public')->attention($data['FromUserName'], 'unsubscribe');
								break;
						}
						break;
				}
			}
		}
	}

	public function pnr(){
		if(IS_POST){
			Vendor('ChinaPnr');
			$pnr = new ChinaPnr();
			$return = $pnr->callback(I('post.'));
			if($return['errcode'] = 0){
				switch($return['data']){
					case '104':
						break;
					case '202':
						if($return['data']['resp_code'] == '202000'){
							$resp = D('Business')->userWithdraw($return['data']);
							if($resp['errcode'] == '0')
								echo $return['recv'].$return['data']['order_id'];
							else
								dump($resp);
						}
						break;
				}
			}else{
				dump($return);
			}
		}
	}

	/**
	 * 创建微信端自定义菜单
	 */
	public function setMenu(){
		header("Content-type: text/html; charset=utf-8");
		Vendor('Weixin');
		$weixin = new Weixin('WKSHOP');
		$menu = array(
			'button'    => array(
				array(
					"type"  => "view",
					'name'  => '商户二维码',
					"url"   => APP_URL.U('Business/Index', array('an' => 'qrcode')),
				),
				array(
					'name'      => '商户功能',
					'sub_button'=> array(
						array(
							"type"  => "view",
							"name"  => "创建商户",
							"url"   => APP_URL.U('Business/Index', array('an' => 'createShop')),
						),
						array(
							"type"  => "view",
							"name"  => "员工绑定",
							"url"   => APP_URL.U('Business/Index', array('an' => 'bindPerson')),
						),
						array(
							"type"  => "view",
							"name"  => "订单列表",
							"url"   => APP_URL.U('Business/Index', array('an' => 'orderList')),
						),
						array(
							"type"  => "view",
							"name"  => "操作日志",
							"url"   => APP_URL.U('Business/Index', array('an' => 'actionLog')),
						),
					)
				),
				array(
					'name'      => '老板后台',
					'sub_button'=> array(
						array(
							"type"  => "view",
							"name"  => "统计报表",
							"url"   => APP_URL.U('Business/Index', array('an' => 'cashRecord')),
						),
						array(
							"type"  => "view",
							"name"  => "员工管理",
							"url"   => APP_URL.U('Business/Index', array('an' => 'presonnel')),
						),
						array(
							"type"  => "view",
							"name"  => "店铺信息管理",
							"url"   => APP_URL.U('Business/Index', array('an' => 'shopInfo')),
						),
						/*array(
							"type"  => "view",
							"name"  => "绑定银行卡",
							"url"   => APP_URL.U('Business/Index', array('an' => 'bindBank')),
						),
						array(
							"type"  => "view",
							"name"  => "修改老板密码",
							"url"   => APP_URL.U('Business/Index', array('an' => 'bossPassword')),
						),*/
					)
				),
			)
		);
		dump($weixin->custom_button('create', $menu));
	}

	public function onePagePostAjax($post){
		switch($post['op']){
			case 'setOrderStatus':
				return D('Business')->orderStatus($post['uuid'], $post['status']);
			case 'getOrderInfo':
				return M('WangkaOrder')->where('uuid="'.$post['uuid'].'"')->find();
		}
	}

	private function message($type, $message){
		switch($type){
			case 'success':
				$this->assign(array(
					'title' => '操作成功啦!',
				    'icon'  => 'success',
					'btn'   => 'primary',
				));
				break;
			case 'error':
				$this->assign(array(
					'title' => '操作失败啦!',
					'icon'  => 'warn',
					'btn'   => 'primary'
				));
				break;
		}
		$this->assign('message', $message);
		$this->display('message');
	}

	public function angularAjax($post){
		$info = session('wkshop');
		switch($post['op']){
			case 'getCategory':
				return D('Business')->getCategory();
			case 'getSession':
				$info = D('Public')->getSession($post['refresh']);
				return $info['sideInfo'];
			case 'createShop':
				return $this->returnMsg(D('Business')->createShop($post));
			case 'qrcode2ShopInfo':
				return D('Business')->qrcode2ShopInfo($post['qrcode']);
			case 'bindPerson':
				return D('Business')->bindPerson($post);
			case 'bindBank':
				return D('Business')->bindBank($post);
			case 'getShopInfo':
				return D('Business')->getShopInfo($post);
			case 'getBalance':
				return D('Business')->balance();
			case 'withdraw':
				return D('Business')->withdraw($post);
			case 'setShopInfo':
				return D('Business')->setShopInfo($post);
			case 'sendSms':
				return $this->sendSms($post, 'WKSHOP');
			case 'getShopOrder':
				return D('Business')->getShopOrder($info['sideInfo']['sid']);
			case 'getShopList':
				return D('Business')->getShopList($post['cid']);
			case 'getPresonList':
				return D('Business')->getPresonList($info['sideInfo']);
			case 'removePreson':
				return D('Business')->removePreson($post['uid'], $info['sideInfo']);
			case 'getLog':
				if($info['sideInfo']['isbind'] != 0)
					return D('Business')->getLog($info['sideInfo']['uid'], $info['sideInfo']['sid']);
				else
					return array('errcode' => '30100');
			case 'getBankData':
				return D('Business')->getBankData($info['sideInfo']['uid']);
			case 'getCashList':
				return D('Business')->getCashList($info['sideInfo']['sid']);
			case 'getAreaData':
				return D('Business')->getAreaData($post['did']);
		}
	}

	protected function validationAjax($post){
		switch($post['op']){
			case 'sms':
				Vendor('CtcSms');
				$sms = new CtcSms();
				return array('errcode' => 0, 'isUnique' => $sms->verify(array('mobile' => $post['mobile'], 'code' => $post['mbcode'])));
		}
	}

	/**
	 * 关注和发信息给微信号时推送的新闻类图文消息的标题/说明/链接地址/图片等信息
	 * @param $param    所关注的微信号: Customer(乐享哇咔)/Business(哇咔商户, 默认)
	 *
	 * @return array
	 */
	protected function sendNews(){
		return array(
			array(
				'Title'         => '如果您还没有自己的商户，点击这里创建自己的商户！',
				'Description'   => '如果您还没有自己的商户，点击这里创建自己的商户！...',
				'Url'           => APP_URL.U('Business/Index', array('an' => 'createShop')),
				'PicUrl'        => APP_URL.'/Public/Custom/Open/images/createShop.jpg',
			),
			array(
				'Title'         => '如果您是老板，请进入这里绑定您的商户！',
				'Description'   => '如果您是老板，请进入这里绑定您的商户！...',
				'Url'           => APP_URL.U('Order/bindShop'),
				'PicUrl'        => APP_URL.'/Public/Custom/Open/images/bindShop.jpg',
			),
			array(
				'Title'         => '如果您是员工，请进入这里加入您所在的商户！',
				'Description'   => '如果您是员工，请进入这里加入您所在的商户！...',
				'Url'           => APP_URL.U('Business/Index', array('an' => 'bindPerson')),
				'PicUrl'        => APP_URL.'/Public/Custom/Open/images/bindPerson.jpg',
			)
		);
	}

	protected function sendSms($post){
		$result = $this->pnr->securityCode(array('user_mobile' => $post['mobile']), '101');
		if($result['errcode'] == '110000')
			return array('errcode' => '0', 'data' => $result['data']);
		else
			return $result;
	}

	public function testCashList(){
		$data[] = array('date' => date('Y年m月d日', time()), 'money' => 4980, 'fees' => 498, 'strike' => 0);
		$data[] = array('date' => date('Y年m月d日', time()-24*60*60), 'money' => 3980, 'fees' => 398, 'strike' => 1);
		$data[] = array('date' => date('Y年m月d日', time()-24*60*60*2), 'money' => 4580, 'fees' => 458, 'strike' => 1);
		$data[] = array('date' => date('Y年m月d日', time()-24*60*60*3), 'money' => 4950, 'fees' => 495, 'strike' => 1);
		$data[] = array('date' => date('Y年m月d日', time()-24*60*60*4), 'money' => 4950, 'fees' => 495, 'strike' => 1);
		$this->ajaxReturn(array('type' => 'Success', 'data' => $data));
	}

	public function test(){
		dump(D('Public')->curd(array(
			'type'  => 'find',
			'model' => M('WangkaBindView'),
			'where' => 'md5key="eed9bd77-134a-11e6-8fcd-02004c4f4f50"'
		)));
	}

	public function test1(){
		dump(D('District')->where('upid=0 AND prov IS NOT NULL')->select());
	}

}